package cn.edu.jnu.booklistmainactivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import cn.edu.jnu.booklistmainactivity.data.BookItem;

public class InputShopItemActivity extends AppCompatActivity {

    public static final int RESULT_CODE_SUCCESS = 666;
    private int position;
    private BookItem intent_book;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*setContentView(R.layout.view_cover_and_info);

        position= this.getIntent().getIntExtra("position",0);
        String title=this.getIntent().getStringExtra("title");
        String author=this.getIntent().getStringExtra("author");
        EditText editTextTitle=findViewById(R.id.book_title_edit_text);
        EditText editTextAuthor=findViewById(R.id.book_author_edit_text);*/
        setContentView(R.layout.activity_add_book1);


        //View v=findViewById(R.id.view_cover_and_info);
        ImageView imageView=findViewById(R.id.book_cover_image_view);
        EditText editTextTitle=findViewById(R.id.book_title_edit_text);
        EditText editTextAuthor=findViewById(R.id.book_author_edit_text);
        EditText editTextTranslator=findViewById(R.id.book_translator_edit_text);
        EditText editTextPublisher=findViewById(R.id.book_publisher_edit_text);
        EditText editTextPubyear=findViewById(R.id.book_pubyear_edit_text);
        EditText editTextPubmonth=findViewById(R.id.book_pubmonth_edit_text);
        EditText editTextIsbn=findViewById(R.id.book_isbn_edit_text);
        //View v2=findViewById(R.id.details);
        EditText editTextNote=findViewById(R.id.details_notes);
        EditText editTextWebsite=findViewById(R.id.details_website);
        position= this.getIntent().getIntExtra("position",0);//如果没有位置(==null)，就默认是0
        intent_book=(BookItem) this.getIntent().getSerializableExtra("book");//必须强制转换

        if(intent_book.getBookName()==null){
            imageView.setBackgroundResource(R.drawable.ic_launcher_background);
        }
        else{
            imageView.setBackgroundResource(R.drawable.book_no_name);
            editTextTitle.setText(intent_book.getBookName());
            editTextAuthor.setText(intent_book.getWriterName());
            editTextTranslator.setText(intent_book.getTranslator());
            editTextPublisher.setText(intent_book.getPublishingHouse());
            editTextPubyear.setText(intent_book.getPublishyear());
            editTextPubmonth.setText(intent_book.getPublishmonth());
            editTextIsbn.setText(intent_book.getIsbn());
        }

        //View title_bar=findViewById(R.id.title_bar);
        Button buttonOk=findViewById(R.id.Edit_Book_Details_Save);
        buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent_book.setBookName(editTextTitle.getText().toString());
                intent_book.setWriterName(editTextAuthor.getText().toString());
                intent_book.setTranslator(editTextTranslator.getText().toString());
                intent_book.setPublishingHouse(editTextPublisher.getText().toString());
                intent_book.setPublishyear(editTextPubyear.getText().toString());
                intent_book.setPublishmonth(editTextPubmonth.getText().toString());
                intent_book.setIsbn(editTextIsbn.getText().toString());

                Intent intent=new Intent();
                Bundle bundle=new Bundle();
                bundle.putInt("position",position);
                bundle.putSerializable("book",intent_book);

                intent.putExtras(bundle);
                setResult(RESULT_CODE_SUCCESS,intent);
                InputShopItemActivity.this.finish();
            }
        });
        Button button_X=findViewById(R.id.Edit_Book_Details_Quit);
        button_X.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击叉叉弹出对话框，询问是否删除
                //通过builder创建一个对话框的模型
                AlertDialog.Builder builder=new AlertDialog.Builder(InputShopItemActivity.this);
                builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("丢弃书籍")
                        .setMessage("我们不会保存您的图书，更改将被丢弃。")
                        .setPositiveButton("丢弃", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //对话框退出
                                dialog.dismiss();
                                //当前页面退出
                                InputShopItemActivity.this.finish();;
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                //利用builder的create（）方法将创建的对话框模型赋值给对话框对象ad
                AlertDialog ad=builder.create();
                //show()方法展示
                ad.show();
            }
        });
    }

}